package com.ydkj.a37e_mall.Classes.Acitivity.BaseActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.ydkj.a37e_mall.Classes.Applicatiom.MyApplication;
import com.ydkj.a37e_mall.Classes.Model.Request.GetExpressReq;
import com.ydkj.a37e_mall.Classes.Model.Response.GetExpressResp;
import com.ydkj.a37e_mall.Classes.Model.Response.GetMallOrderListResp;
import com.ydkj.a37e_mall.R;
import com.ydkj.a37e_mall.Resource.Utils.Async_http_get;
import com.ydkj.a37e_mall.Resource.Utils.CommonUtil;
import com.ydkj.a37e_mall.Resource.Utils.Const;
import com.ydkj.a37e_mall.Resource.Utils.GsonImpl;
import com.ydkj.a37e_mall.Resource.Utils.Rsa;
import com.ydkj.a37e_mall.Resource.Utils.SharedPreferencesUtil;
import com.ydkj.a37e_mall.Resource.View.CustomProgressDialog;
import com.ydkj.a37e_mall.Resource.View.ListViewForScrollView;
import com.ydkj.a37e_mall.Resource.View.StatusBarUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;


/**
 * Created by hong on 2016/3/17.
 * <p/>
 * 查看物流
 */
public class SeeLogistics extends Activity {

    private Activity mActivity;
    private MyApplication myApplication;
    private SharedPreferencesUtil sharedPreferencesUtil;
    private List<GetMallOrderListResp.ResultEntity.DataEntity> intentList;
    private List<GetExpressResp.ResultBean.DataBean> logisticsList;
    private TextView tv_order_num;
    private ImageView img_back, img_product;
    private ScrollView scrollView;
    private TextView tv_logistics_status, tv_logistics_company, tv_courier_number, tv_logistics_track;
    private LinearLayout lly_logistics_track;
    private ListViewForScrollView lv_logistics_information;

    private LogisticsAdapter logisticsAdapter;
    private String id;//订单id
    private String secret;

    private int size;

    private String[] name = {"", "", "", ""};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.see_logistics);
        mActivity = this;
        init();
//        initWindow();//沉浸式状态栏
    }

    private void initWindow() {
        StatusBarUtil.setColor(mActivity, getResources().getColor(R.color.e50011), 0);
    }

    private void init() {
        myApplication = MyApplication.getInstance();
        sharedPreferencesUtil = SharedPreferencesUtil.getInstance(mActivity);
        tv_order_num = (TextView) mActivity.findViewById(R.id.tv_order_num);
        img_back = (ImageView) mActivity.findViewById(R.id.img_back);
        img_product = (ImageView) mActivity.findViewById(R.id.img_product);
        scrollView = (ScrollView) mActivity.findViewById(R.id.scrollview);
        tv_logistics_status = (TextView) mActivity.findViewById(R.id.tv_logistics_status);
        tv_logistics_company = (TextView) mActivity.findViewById(R.id.tv_logistics_company);
        tv_courier_number = (TextView) mActivity.findViewById(R.id.tv_courier_number);
        tv_logistics_track = (TextView) mActivity.findViewById(R.id.tv_logistics_track);
        lly_logistics_track = (LinearLayout) mActivity.findViewById(R.id.lly_logistics_track);
        lv_logistics_information = (ListViewForScrollView) mActivity.findViewById(R.id.lv_logistics_information);

        lv_logistics_information.setFocusable(false);

        intentList = new ArrayList<GetMallOrderListResp.ResultEntity.DataEntity>();
        logisticsList = new ArrayList<GetExpressResp.ResultBean.DataBean>();
        intentList.clear();
        if (this.getIntent().getSerializableExtra("intentList") != null) {
            intentList = (ArrayList<GetMallOrderListResp.ResultEntity.DataEntity>) getIntent().getSerializableExtra("intentList");
            id = intentList.get(0).getId();
            Log.e("url", "id==" + id);
        }

        tv_order_num.setText(intentList.get(0).getSn());

//        ImageLoader.getInstance().displayImage(intentList.get(0).getInfo().get(0).getThumb(), img_product);


        GetExpress();//获取快递


        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    private void setAdapter() {
        if (logisticsAdapter == null) {
            logisticsAdapter = new LogisticsAdapter(logisticsList.get(0).getDetail());
            lv_logistics_information.setAdapter(logisticsAdapter);
        } else {
            logisticsAdapter.notifyDataSetChanged();
        }
    }

    /**
     * 获取快递情况
     */
    private void GetExpress() {
        CustomProgressDialog.createDialog(mActivity).Show();
        GetExpressReq req = new GetExpressReq();
        req.setUuid(myApplication.getDataEntity().getUuid());
        req.setId(id);
        try {
            String source = req.getString();
            Rsa rsa = new Rsa();
            secret = rsa.encryptByPublic(source);
            Log.e("url", "source==" + source);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String url = Const.COMMON_ADDRESS + req.urlString();
        RequestParams params = new RequestParams();
        params.put("token", sharedPreferencesUtil.getAccessToken());
        params.put("secret", secret);
        Async_http_get.get(url, params, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                CustomProgressDialog.Dismiss();
                Log.e("url", "all_mall_order_json==" + response);
                String errno = response.optString("errno");
                String errmsg = response.optString("errmsg");
                if (errno.equals("000")) {
                    GetExpressResp resp = GsonImpl.get().toObject(response.toString(), GetExpressResp.class);
                    logisticsList.clear();
                    logisticsList.add(resp.getResult().getData());

                    if (logisticsList.size() != 0) {
                        tv_logistics_status.setText(resp.getResult().getData().getStats());
                        tv_logistics_company.setText(resp.getResult().getData().getCompany());
                        tv_courier_number.setText(resp.getResult().getData().getNo());

                    } else {
                        tv_logistics_track.setText("暂无物流消息");
                    }

                    setAdapter();
                } else if (errno.equals("999")) {
                    CommonUtil.showToast(mActivity, errmsg);
                    Intent intent = new Intent(mActivity, LoginActivity.class);
                    startActivityForResult(intent, 3);
                    Log.e("url", "errmsg==" + errmsg);
                } else {
                    CommonUtil.showToast(mActivity, errmsg);
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                super.onFailure(statusCode, headers, responseString, throwable);
                CustomProgressDialog.Dismiss();
                CommonUtil.showToast(mActivity, "网络连接失败");
            }
        });
    }

    private class LogisticsAdapter extends BaseAdapter {

        private List<GetExpressResp.ResultBean.DataBean.DetailBean> logisticsList;

        private LogisticsAdapter(List<GetExpressResp.ResultBean.DataBean.DetailBean> infos) {
            if (infos == null) {
                infos = new ArrayList<GetExpressResp.ResultBean.DataBean.DetailBean>();
            }
            logisticsList = infos;
        }

        @Override
        public int getCount() {
            return logisticsList.size();
        }

        @Override
        public GetExpressResp.ResultBean.DataBean.DetailBean getItem(int position) {
            return logisticsList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder vh = null;
            if (convertView == null) {
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.see_logistics_listitem, null);
                vh = new ViewHolder();
                vh.img_dot = (ImageView) convertView.findViewById(R.id.img_dot);
                vh.tv_logistics_process = (TextView) convertView.findViewById(R.id.tv_logistics_process);
                vh.tv_logistics_time = (TextView) convertView.findViewById(R.id.tv_logistics_time);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            convertView.setTag(vh);
            convertView.setClickable(true);

            vh.tv_logistics_process.setText(getItem(position).getContext());
            vh.tv_logistics_time.setText(getItem(position).getTime());
            if (position == 0) {
                vh.img_dot.setImageDrawable(getResources().getDrawable(R.mipmap.dot));
                vh.tv_logistics_process.setTextColor(getResources().getColor(R.color.ff6600));
                vh.tv_logistics_time.setTextColor(getResources().getColor(R.color.ff6600));
            }

            return convertView;
        }

        private class ViewHolder {
            private ImageView img_dot;
            private TextView tv_logistics_process, tv_logistics_time;
        }
    }
}
